<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发送 POST 请求</title>
    <style>
       #box{
           width: 200px;
           height: 200px;
           border: solid 1px #666;
       }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        //获取元素
        let box = document.querySelector('#box');

        //绑定鼠标移入事件
        box.onmouseover = function(){
            //1. 实例化对象
            let xhr = new XMLHttpRequest();

            //2. 初始化  设置请求行
            xhr.open('post', 'http://localhost/server');// 127.0.0.1  localhost

            //set 设置  request 请求  header 头
            xhr.setRequestHeader('class', 'H5211227');
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') // 设置请求体内容的类型

            //3. 发送  设置请求体
            xhr.send('a=100&b=200');

            //4. 绑定事件
            xhr.onreadystatechange = function(){
                //判断
                if(xhr.readyState === 4){
                    //输出响应体
                    // console.log(xhr.response);
                    //修改 box 的文本内容
                    box.innerHTML = xhr.response;
                }
            }

        }
    </script>
</body>
</html>